package com.yisu.auction.api.web.interceptor;


import com.yisu.auction.ewf.common.Constants;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.json.FastJson;
import com.yisu.auction.ewf.common.web.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by LKL on 2016/8/25.
 */
public class HeaderInterceptor extends HandlerInterceptorAdapter {
    private static final Logger logger = LoggerFactory.getLogger(HeaderInterceptor.class);
    private static final String EXCLUDE_URI_PAY_NOTIFY = "/order/PingNotify";
    private static final String EXCLUDE_URI_WeChat_CallBack = "/wechat/UserInfoCallback";
    private static final String EXCLUDE_URI_WECHAT_AUTHENTIACTIONINFO= "/wechat/authenticationInfo";
    private static final String EXCLUDE_URI_WECHAT_HEALTHCHECKSTATUS= "/healthCheck/checkStatus"; //健康轮询检查
    private static final String EXCLUDE_URI_ORDER_REFUND_APP= "/order/orderRefund";
    private static final String EXCLUDE_URI_APP_DOWNLOAD= "/app/appDownLoad";

    private static final String X_CLIENT_AGENT ="X-Client-Agent";
    private static final String X_API_VERSION ="X-APIVersion";
    private static final String X_CLIENT_ID ="X-Client-ID";
    private static final String X_USER_ID ="X-User-ID";
    private static final String X_LONG_TOKEN ="X-Long-Token";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        logger.info("Request uri: {}", requestURI);
        if (requestURI.startsWith(EXCLUDE_URI_PAY_NOTIFY) ||requestURI.startsWith(EXCLUDE_URI_ORDER_REFUND_APP) || requestURI.startsWith(EXCLUDE_URI_WeChat_CallBack)
                ||requestURI.startsWith(EXCLUDE_URI_WECHAT_AUTHENTIACTIONINFO)||requestURI.startsWith(EXCLUDE_URI_WECHAT_HEALTHCHECKSTATUS)||
        requestURI.startsWith(EXCLUDE_URI_APP_DOWNLOAD) ) {
            return true;
        }

       /* CommonHeader header = new CommonHeader();
        header.setClientAgent(request.getHeader(X_CLIENT_AGENT));
        header.setAppVersion(request.getHeader(X_API_VERSION));
        header.setClientId(request.getHeader(X_CLIENT_ID));
        header.setUserId(request.getHeader(X_USER_ID));
        header.setToken(request.getHeader(X_LONG_TOKEN));

        logger.info("Request header {}", header);

        if(StringUtils.isEmpty(header.getAppVersion()) || StringUtils.isEmpty(header.getClientAgent())
                || StringUtils.isEmpty(header.getClientId())) {
            logger.error("Request header is wrong.");
            writeResponse(response);
            return false;
        }*/

      //  request.setAttribute(Constants.X_HEADER_INFO, header);
        return true;
    }

//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//
//    }

    private void writeResponse(HttpServletResponse response) throws Exception{
        response.setHeader(Constants.REQUEST_RESULT_CODE, String.valueOf(ResponseCode.SUCCESS.code));
        response.setHeader("Content-Type", "application/json");
        response.setCharacterEncoding("UTF-8");
        JsonResult result = new JsonResult(ResponseCode.HEADER_ERROR.code, ResponseCode.HEADER_ERROR.msg);
        response.getWriter().write(FastJson.toJson(result));
    }

}